﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Cashier.utils;
using Cashier_Model;
using Cashier_Service;

namespace Cashier.rights
{
    public partial class PurviewAddForm : Form
    {
        private bool isSave;
        private RightsForm rightsForm;
        private Purview purview;
        private PurviewService purviewService;
        private long purviewid;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rightsForm"></param>
        public PurviewAddForm(RightsForm rightsForm, long purviewid)
        {
            InitializeComponent();
            this.rightsForm = rightsForm;
            this.purviewService = new PurviewService();
            this.purviewid = purviewid;

            if (purviewid > 0)
            {
                this.purview = purviewService.GetPurview(purviewid);
                FormUtil.InitialFormData(this.purview, this.groupBox1, 1);
            }
        }

        #region 功能方法实现

        /// <summary>
        /// 保存权限
        /// </summary>
        private void SavePurview()
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this.groupBox1, list, 1);
            if (list.Count > 0)
                return;

            if (purview == null)
                purview = new Purview();
            FormUtil.GetFormData(purview, this.groupBox1, 1);

            bool issuccess = false;
            if (purview.Id > 0)
            {
                int flag = this.purviewService.UpdatePurview(purview);
                if (flag > 0)
                    issuccess = true;
            }
            else
            {
                long id = this.purviewService.InsertPurview(purview);
                if (id > 0)
                {
                    issuccess = true;
                    purview.Id = id;
                }
            }

            if (issuccess)
            {
                DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    purview = null;
                    FormUtil.ClearFormData(this.groupBox1, 1);
                    rightsForm.LoadPurviewData();
                }
                else
                {
                    this.rightsForm.LoadPurviewData();
                    this.isSave = false;
                    this.Close();
                }
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.isSave = true;
            this.SavePurview();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.isSave = false;
            this.Close();
        }

        #endregion

        #region 重写函数

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.isSave = false;
                    this.Close();
                    break;
            }
            return b;
        }

        #endregion

    }
}
